/* *****************************************************************************
 * JFab (http://code.google.com/p/jfab)
 * Copyright (c) 2011-2012 JFab.org
 * Admin jfab@jeffreythompson.net
 *
 * See the file "LICENSE.txt" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 * *****************************************************************************/

package org.jfab.core.io;

import java.io.Writer;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.jfab.core.model.CGSpec;

/**
 * Provides a writer for code generator XML documents.
 */
public class CGSpecWriter
{
    /**
     * Write a code generator XML document.
     * 
     * @param writer Writer.
     * @param cgSpec Code generator root.
     */
    public void write(final Writer writer, final CGSpec cgSpec)
    {
        try
        {
            final JAXBContext jaxbContext = JAXBContext.newInstance(Constants.PACKAGE_NAME);
            final Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty("jaxb.formatted.output", true);
            marshaller.setProperty("jaxb.schemaLocation", Constants.SCHEMA_LOCATION);
            marshaller.marshal(cgSpec, writer);
        }
        catch (final JAXBException e)
        {
            throw new IOWriterException(e);
        }
    }
}
